package com.tqjc.execute.thread.aysn;

import com.tqjc.execute.cache.TaskStatus;
import com.tqjc.execute.cache.entity.WorkStatus;
import com.tqjc.execute.consts.WorkConst;
import com.tqjc.execute.exception.TaskRunningException;
import com.tqjc.execute.utils.HttpPLCUtils;
import com.tqjc.execute.work.CraneWork;
import com.tqjc.system.core.util.SpringUtils;
import com.tqjc.system.system.util.SystemUtils;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.client.RestTemplate;

/**
 * @Author: 86195
 * @CreateTime: 2024-07-11  17:16
 * @Description: TODO
 * @Version: 1.0
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Slf4j
public class AysnEasyPlaceThread extends Thread{
    //天车类型
    private String craneType;

    private String host;

    private String stockNo;


    private CraneWork craneWorkService;

    @Override
    public void run() {
        log.info("天车：【{}】一键放料异步线程开始运行,垛位信息是：{}",craneType,stockNo);
        try {
            RestTemplate restTemplate = SpringUtils.getBean(RestTemplate.class);
            TaskStatus taskStatus = SpringUtils.getBean(TaskStatus.class);
            WorkStatus workStatus = taskStatus.getWorkStatusByCraneType(craneType);

            workStatus.setNowStatus("运行中");
            workStatus.setNowTask("一键放料");
            workStatus.setUnnormalStatus("");
            workStatus.setNowMode("2#横移".equals(craneType)?"online":"offline");
            taskStatus.setWorkStatusByCraneType(craneType,workStatus);
            //设置任务标识
            craneWorkService.setSemiAutoRunFlag(true);
            log.info("天车：【{}】一键放料异步线程开始写点位",craneType);
            if("11C北".equals(craneType) || "11C南".equals(craneType)){
                //首先写垛位信息
                if("1".equals(stockNo)){
                    HttpPLCUtils.plcWritebcPlus(restTemplate,host,craneType,"DB20.8.7",HttpPLCUtils.dataTypeList.get("bool"),"TRUE","垛位1",false);
                    HttpPLCUtils.plcWritebcPlus(restTemplate,host,craneType,"DB20.9",HttpPLCUtils.dataTypeList.get("bool"),"FALSE","垛位2",false);
                }else {
                    HttpPLCUtils.plcWritebcPlus(restTemplate,host,craneType,"DB20.9",HttpPLCUtils.dataTypeList.get("bool"),"TRUE","垛位2",false);
                    HttpPLCUtils.plcWritebcPlus(restTemplate,host,craneType,"DB20.8.7",HttpPLCUtils.dataTypeList.get("bool"),"FALSE","垛位1",false);
                }

            } else {
                HttpPLCUtils.plcWritebcPlus(restTemplate,host,craneType,"DB20.8.7",HttpPLCUtils.dataTypeList.get("bool"),"TRUE","垛位1",false);
            }

            //1.写值
            HttpPLCUtils.plcWritebcPlus(restTemplate,host,craneType,WorkConst.EASY_PLACE_DB,WorkConst.DATATYPE_BOOL,"true",WorkConst.EASY_PLACE_LABEL,false);
            //2.写值成功后，复位
            Thread.sleep(200);
            HttpPLCUtils.plcWritebcPlus(restTemplate,host,craneType,WorkConst.EASY_PLACE_DB,WorkConst.DATATYPE_BOOL,"false",WorkConst.EASY_PLACE_LABEL,false);
            log.info("天车：【{}】一键放料异步线程完成写点位",craneType);
            long start = System.currentTimeMillis();
            while (true){
                String finishFlag = HttpPLCUtils.plcReadbc(restTemplate, host, craneType, "一键放料运行中", false);
                if("false".equalsIgnoreCase(finishFlag)){
                    log.info("天车：【{}】一键放料任务完成",craneType);
                    break;
                }
                String taskFlag = HttpPLCUtils.plcReadbc(restTemplate, host, craneType, "任务中止", false);
                if("true".equalsIgnoreCase(taskFlag)){
                    log.info("天车执行自动放板步骤时，监听到任务中止信号");
                    taskStatus.setTaskStatusByCraneType(craneType,"一键放料任务人工停止");
                    throw new TaskRunningException("任务人工停止",craneType);
                }
                if(System.currentTimeMillis() - start > 5*60*1000){
                    log.error("天车：【{}】一键放料任务超时",craneType);
                    taskStatus.setTaskStatusByCraneType(craneType,"一键放料任务超时");
                    break;
                }


            }

        } catch (Exception e){
            log.error("天车：【{}】一键放料异步线程运行出现异常",craneType,e);

        }finally {
            craneWorkService.setSemiAutoRunFlag(false);
        }



        log.info("天车：【{}】一键放料异步线程远程完成",craneType);


    }
}
